/* Copyright  2009-2010
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/*
   @filename: varstring.h
*/

#ifndef _SERVER_FIELD_VARSTRING
#define _SERVER_FIELD_VARSTRING

#include <field/longstr.h>
#include <string>

class Field_varstring :public Field_longstr {
public:

  using Field::store;
  using Field::pack;
  using Field::unpack;
  using Field::val_int;
  using Field::val_str;

  /*
    The maximum space available in a Field_varstring, in bytes. See
    length_bytes.
  */
  static const uint32_t MAX_SIZE;
  /* Store number of bytes used to store length (1 or 2) */
  uint32_t length_bytes;

  Field_varstring(unsigned char *ptr_arg,
                  uint32_t len_arg, uint32_t length_bytes_arg,
                  unsigned char *null_ptr_arg, unsigned char null_bit_arg,
                  enum utype unireg_check_arg, const char *field_name_arg,
                  TABLE_SHARE *share,
                  const CHARSET_INFO * const cs);

  Field_varstring(uint32_t len_arg,bool maybe_null_arg,
                  const char *field_name_arg,
                  TABLE_SHARE *share,
                  const CHARSET_INFO * const cs);

  inline enum_field_types type() const { return COLLAPSAR_TYPE_VARCHAR; }

  enum ha_base_keytype key_type() const;
  inline uint32_t row_pack_length() { return field_length; }
  inline bool zero_pack() const { return 0; }
  inline int  reset(void) { ::memset(ptr, 0, field_length+length_bytes); return 0; }
  inline uint32_t pack_length() const { return (uint32_t) field_length+length_bytes; }
  inline uint32_t key_length() const { return (uint32_t) field_length; }
  inline uint32_t sort_length() const
  {
    return (uint32_t) field_length + (field_charset == &my_charset_bin ?
                                    length_bytes : 0);
  }
  int  store(const char *to,uint32_t length, const CHARSET_INFO * const charset);


  int  store(int64_t nr, bool unsigned_val);
  inline int  store(double nr) { return Field_str::store(nr); } /* QQ: To be deleted */
  double val_real(void);
  int64_t val_int(void);
  String *val_str(String*,String *);
  inline String *val_str(String *str) { return val_str(str, str); }
  db_decimal *val_decimal(db_decimal *);
  int cmp_max(const unsigned char *, const unsigned char *, uint32_t max_length);
  inline  int cmp(const unsigned char *str) { return cmp(ptr,str); }
  inline int cmp(const unsigned char *a,const unsigned char *b)
  {
    return cmp_max(a, b, UINT32_MAX);
  }
  void sort_string(unsigned char *buff,uint32_t length);
  uint32_t get_key_image(unsigned char *buff,uint32_t length, imagetype type);
  uint32_t get_key_image(std::basic_string <unsigned char> &buff,
                         uint32_t length, imagetype type);
  void set_key_image(const unsigned char *buff,uint32_t length);
  void sql_type(String &str) const;
  virtual unsigned char *pack(unsigned char *to, const unsigned char *from,
                              uint32_t max_length,
                              bool low_byte_first);

  unsigned char *pack_key(unsigned char *to, const unsigned char *from, uint32_t max_length, bool low_byte_first);
  unsigned char *pack_key_from_key_image(unsigned char* to, const unsigned char *from,
                                 uint32_t max_length, bool low_byte_first);
  virtual const unsigned char *unpack(unsigned char* to,
                                      const unsigned char *from,
                                      uint32_t param_data,
                                      bool low_byte_first);

  const unsigned char *unpack_key(unsigned char* to, const unsigned char *from,
                          uint32_t max_length, bool low_byte_first);
  int pack_cmp(const unsigned char *a, const unsigned char *b, uint32_t key_length,
               bool insert_or_update);
  int pack_cmp(const unsigned char *b, uint32_t key_length,bool insert_or_update);
  int cmp_binary(const unsigned char *a,const unsigned char *b,
                                uint32_t max_length=UINT32_MAX);
  int key_cmp(const unsigned char *,const unsigned char*);
  int key_cmp(const unsigned char *str, uint32_t length);
  uint32_t packed_col_length(const unsigned char *to, uint32_t length);
  uint32_t max_packed_col_length(uint32_t max_length);
  uint32_t data_length();
  uint32_t used_length();
  uint32_t size_of() const { return sizeof(*this); }
  inline enum_field_types real_type() const       { return COLLAPSAR_TYPE_VARCHAR; }
  inline bool has_charset(void) const
  { return charset() == &my_charset_bin ? false : true; }
  Field *new_field(MEM_ROOT *root, Table *new_table, bool keep_type);
  Field *new_key_field(MEM_ROOT *root, Table *new_table,
                       unsigned char *new_ptr, unsigned char *new_null_ptr,
                       uint32_t new_null_bit);
  uint32_t is_equal(Create_field *new_field);
  void hash(uint32_t *nr, uint32_t *nr2);
private:
  int do_save_field_metadata(unsigned char *first_byte);
};

#endif

